package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.entity.MyAttentionBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/7/16 0016.
 */

public class MyAttentionModel implements MyAttentionModelImpl {

    private static final String TAG = "MyAttentionModel";
    private OnRequestListener mListener;

    public MyAttentionModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getMyAttentionResult(String id) {
        Observable<MyAttentionBean> result = DataManager.getMyAttentionResult(
                id
        );
        Observable<MyAttentionBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<MyAttentionBean>() {

            @Override
            public void onNext(@NonNull MyAttentionBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getMyFansResult(String id) {
        Observable<MyAttentionBean> result = DataManager.getMyFansResult(
                id
        );
        Observable<MyAttentionBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<MyAttentionBean>() {

            @Override
            public void onNext(@NonNull MyAttentionBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void requestFollow(String mUid, final MyAttentionBean.DataBean.ConcernBean bean, final int position, int pageType) {
        Observable<FollowResultBean> result = DataManager.getFollowResult(
                mUid,
                (pageType == 0) ? bean.getToUid() : bean.getFromUid()
        );
        Observable<FollowResultBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<FollowResultBean>() {

            @Override
            public void onNext(@NonNull FollowResultBean resultBean) {
                if (mListener != null) {
                    mListener.onFollowSuccess(resultBean, position, bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onFollowError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {

        void onSuccess(MyAttentionBean bean);

        void onError(Throwable throwable);

        void onFollowSuccess(FollowResultBean bean, int position, MyAttentionBean.DataBean.ConcernBean concernBean);

        void onFollowError(Throwable throwable);
    }
}
